<template>
  <el-drawer
    @close="close"
    :visible.sync="dialogVisible"
    :title="title"
    :size="500"
  >
    <el-row :gutter="10" class="box-card">
      <el-form ref="elForm" :model="formData" :rules="rules" size="small" label-width="90px">
        <el-col :span="24">
          <el-form-item label="商品名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入商品名称" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
         <el-col :span="24">
          <el-form-item label="市场价" prop="price">
            <el-input v-model="formData.price" placeholder="请输入市场价" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
         <el-col :span="24">
          <el-form-item label="积分价格" prop="jfPrice">
            <el-input v-model="formData.jfPrice" placeholder="请输入积分价格" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="商品封面"
            prop="picUrl"
          >
            <div
              class="upload-box"
            >
              <el-upload
                class="avatar-uploader"
                :action="upload.uploadPermissionImage"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :multiple="false"
                :headers="upload.uploadHeader"
                accept=".jpg,.jpeg,.png,.gif"
                :data="{fileSize: 10000}"
              >
                <img
                  v-if="formData.picUrl"
                  :src="formData.picUrl | getImgUrl"
                  class="avatar"
                >
                <i
                  v-else
                  class="el-icon-plus avatar-uploader-icon"
                />
              </el-upload>
            </div>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div class="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handelConfirm">确定</el-button>
    </div>
  </el-drawer>
</template>
<script>
import { jfcsSave, jfcsUpdate } from '@/api/jfcs'
import { getToken } from '@/utils/auth'
export default {
  props: [],
  data () {
    return {
      dialogVisible: false,
      formData: {
        name: '', // 商品名称
        picUrl: '', // 商品封面
        price: '', // 市场价
        jfPrice: ''
      },
      upload: {
        uploadPermissionImage: process.env.VUE_APP_BASE_API + '/file/uploadImg',
        uploadHeader: {
          'jwt-token': getToken()
        }
      },
      rules: {
        name: [{
          required: true,
          message: '请输入商品名称',
          trigger: 'blur'
        }],
        price: [{
          required: true,
          message: '请输入市场价',
          trigger: 'blur'
        }],
        picUrl: [{
          required: true,
          message: '请上传商品封面',
          trigger: 'blur'
        }],
        jfPrice: [{
          required: true,
          message: '请输入积分价格',
          trigger: 'blur'
        }]
      },
      deptOptions: [],
      rolesOptions: [],
      title: '新增积分超市',
      id: null,
      isEdit: false
    }
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    /**
     *
     * @param {*} isEdit 是否编辑
     * @param {*} data 编辑时需要的数据
     */
    async isShow (data, isEdit = false) {
      this.dialogVisible = true
      this.title = isEdit ? '编辑积分超市' : '新增积分超市'
      this.isEdit = isEdit
      await this.$nextTick()
      if (isEdit) {
        const formData = this.formData
        for (const key in formData) {
          formData[key] = data[key]
        }
        this.id = data.id
      }
    },
    handleAvatarSuccess (file) {
      if (file.code === 200) {
        this.formData.picUrl = file.data.url
      } else {
        this.$notify.error(file.message)
      }
    },
    close () {
      this.$refs.elForm.resetFields()
      this.dialogVisible = false
    },
    handelConfirm () {
      this.$refs.elForm.validate(async valid => {
        if (!valid) return
        this.isEdit ? (await jfcsUpdate({ ...this.formData, id: this.id })) : (await jfcsSave(this.formData))
        this.$notify.success({
          title: `完成${this.title}`
        })
        this.$emit('update:visible')
        this.close()
      })
    }
  }
}

</script>
<style lang="scss" scoped>
.box-card {
  margin-bottom: 10px;
  padding-right: 20px;
  width: 100%;
  padding-left: 10px;
}
.footer {
  display: flex;
  justify-content: space-around;
  margin-bottom: 40px;
  padding-left: 100px;
  padding-right: 20px;
  .el-button {
    width: 100%;
  }
}
.avatar-uploader {
  ::v-deep .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    &:hover {
      border-color: #409eff;
    }
  }
}
.avatar-uploader-icon {
  font-size: 20px;
  //color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>
